package com.zhentao.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.entity.product.Category;
import com.zhentao.listener.ExcelListener;
import com.zhentao.service.CategoryService;
import com.zhentao.mapper.CategoryMapper;
import com.zhentao.vo.product.CategoryExcelVo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【category(商品分类)】的数据库操作Service实现
* @createDate 2024-09-02 10:28:26
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Resource
    private CategoryMapper categoryMapper;
    /**
     * 查询商品分类列表
     * @param parentId
     * @return
     */
    @Override
    public List<Category> findByParentId(Long parentId) {
        // 根据分类id查询它下面的所有的子分类数据
        List<Category> categoryList = categoryMapper.selectList(Wrappers.<Category>lambdaQuery().eq(Category::getParentId,parentId));
        if(!CollectionUtils.isEmpty(categoryList)) {
            // 遍历分类的集合，获取每一个分类数据
            categoryList.forEach(item -> {
                // 查询该分类下子分类的数量
                int count = categoryMapper.countByParentId(item.getId());
                if(count > 0) {
                    item.setHasChildren(true);
                } else {
                    item.setHasChildren(false);
                }
            });
        }
        return categoryList;
    }

    /**
     * 导出
     * @param response
     */
    @SneakyThrows
    @Override
    public void exportData(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("分类数据", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库中的数据
        List<Category> categoryList = categoryMapper.selectList(Wrappers.<Category>lambdaQuery().eq(Category::getIsDeleted,0));
        List<CategoryExcelVo> categoryExcelVoList = new ArrayList<>(categoryList.size());
        //查询数据库中查询到的是Category对象转换成CategoryExcelVo对象
        for (Category category : categoryList) {
            CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
            BeanUtils.copyProperties(category,categoryExcelVo,CategoryExcelVo.class);
            categoryExcelVoList.add(categoryExcelVo);
        }
        //写出数据到浏览器端
        EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class).sheet("分类数据").doWrite(categoryExcelVoList);
    }

    /**
     * 导入
     * @param file
     */
    @SneakyThrows
    @Override
    public void importData(MultipartFile file) {
        //创建监听器对象，传递mapper对象
        ExcelListener<CategoryExcelVo> excelListener = new ExcelListener<>(categoryMapper);
        //调用read方法读取excel数据
        EasyExcel.read(file.getInputStream(),
                CategoryExcelVo.class,
                excelListener).sheet().doRead();
    }
}